<template>
  <div class="app-container">
    <div class="flex-box">
      <div class="item">
        <el-button icon="el-icon-circle-plus-outline" @click="addShow">添加类别</el-button>
      </div>
      <div class="item">
        <span>总共有{{ total }}条数据</span>
      </div>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" class="no-r-b"
      :header-cell-style="{background:'#FAFAFA',color:'#666666'}">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="英文名">
        <template slot-scope="scope">
          <span>{{ scope.row.catename }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="中文名">
        <template slot-scope="scope">
          <span>{{ scope.row.catename_zn }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建日期">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="editShow(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!--列表数据添加/编辑开始  -->
    <el-dialog v-if="dialogFormVisible" :title="dialogTitle" :visible.sync="dialogFormVisible" center width="95%" class="user">
      <el-form ref="ruleForm" :model="formRow" label-width="100px">
        <el-form-item label="类别英文名">
          <el-input v-model="formRow.catename" placeholder="类别英文名" auto-complete="off" />
        </el-form-item>
        <el-form-item label="类别中文名">
          <el-input v-model="formRow.catename_zn" placeholder="类别中文名" auto-complete="off" />
        </el-form-item>
        <!-- <el-form-item> -->
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <!-- </el-form-item> -->
      </el-form>
    </el-dialog>
    <!--列表数据添加/编辑结束  -->
  </div>
</template>

<script>
import { getCates, addCate, editCate } from '@/api/prod'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { Message } from 'element-ui'

export default {
  name: 'CateList',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      // 弹窗标题
      dialogTitle: '',
      dialogFormVisible: false,
      // 表单数据
      formRow: {
        catename: '',
        catename_zn: ''
      }
    }
  },
  computed: {
    // minMoney: {
    //   get() {
    //     return this.formRow.min / 100
    //   },
    //   set(val) {
    //     this.formRow.min = val * 100
    //   }
    // },
    // maxMoney: {
    //   get() {
    //     return this.formRow.max / 100
    //   },
    //   set(val) {
    //     this.formRow.max = val * 100
    //   }
    // }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.listLoading = true
      getCates(this.listQuery).then(response => {
        const { data } = response
        this.list = data.list
        this.total = data.total
        this.listLoading = false
        console.log(data, 'getList')
      })
    },

    // 表单提交方法
    submitForm(formName) {
      console.log(this.formRow, 'submitForm')
      if (!this.formRow.id) { // 新增数据
        addCate(this.formRow).then(res => {
          Message({
            message: '保存成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.dialogFormVisible = false
          this.getList()
        })
      } else { // 编辑数据
        editCate(this.formRow).then(res => {
          Message({
            message: '保存成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.dialogFormVisible = false
          this.getList()
        })
      }
    },
    // 搜索日期变化操作方法
    dateChange(options) {
      console.log(options, 'dateChange')
    },
    // 添加
    addShow() {
      this.formRow = {
        catename: '',
        catename_zn: ''
      }
      this.dialogTitle = '类别添加'
      this.dialogFormVisible = true
    },
    // 编辑
    editShow(row) {
      this.formRow = row
      this.dialogTitle = '类别编辑'
      this.dialogFormVisible = true
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
